#include "timecomputer.h"
#include "./ui_timecomputer.h"

#include<QString>
#include<QLabel>
#include<QPixmap>
#include<QPalette>
#include<QImageReader>
#include <QMediaPlayer>
#include <QAudioOutput>
#include<QMediaFormat>
#include<QDebug>
#include<QUrl>
#include<QSoundEffect>

TimeComputer::TimeComputer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TimeComputer)
{
    ui->setupUi(this);

    //设置ComboBox的属性
    QStringList Timetype = {"秒","分","时"};
    QStringList Caculatetype = {"+","-"};
    ui->CaculateType->addItems(Caculatetype);
    ui->TimeType->addItems(Timetype);
    ui->TimeType2->addItems(Timetype);

    this->setWindowTitle("时间计算机");
}

void TimeComputer::SetImage(QWidget* &window,const QString &filename){
    //导入图片
    QPixmap pixmap(filename);
    if (pixmap.isNull()) {
        qDebug() << "错误：图片加载失败，请检查格式或路径：" << filename;
        return;
    }

    //设置图片窗口
    QWidget *Imgwindow = new QWidget(window);
    Imgwindow->setGeometry(75,0,100,100);
    Imgwindow->setAutoFillBackground(true);


    QSize windowSize = Imgwindow->size();
    //调整图片大小
    QPixmap scalePixmap = pixmap.scaled(windowSize,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

    //设置一个QPalette对象并设置背景——调色板
    QPalette palette = Imgwindow->palette();
    palette.setBrush(QPalette::Window,QBrush(scalePixmap));

    //将调色板运用到widget上
    Imgwindow->setPalette(palette);
}

TimeComputer::~TimeComputer()
{
    delete ui;
}

//计算时间
QString Caculate(int time1,int time2,int cur1,int cur2,int ca_type){
    std::vector<int>Time(3,0);
    std::vector<QString>Type={"秒","分","时"};
    auto cac=[&](int time,int cur)->int{
        for(int i=cur;i>0;i--){
            time*=60;
        }
        return time;
    };
    int value=ca_type==0?cac(time1,cur1)+cac(time2,cur2):cac(time1,cur1)-cac(time2,cur2);

    //当结果为负值时，返回
    if(value<0)return "结果为负值";
    if(value==0)return "0秒";

    int i=0;
    while(value>0){
        Time[i]+=value%60;
        value/=60;
        i++;
    }

    QString str="";
    for(int i=2;i>=0;i--){
        if(Time[i]==0)continue;
        str+=QString::number(Time[i])+Type[i];
    }

    if(str=="2分30秒"||str=="2时30分"){
        return "只因你太美";
    }

    return str;
}


void TimeComputer::on_pushButton_clicked()
{
    //设置答案窗口
    QWidget* answerwindow = new QWidget();

    answerwindow->setMaximumSize(250,150);
    answerwindow->setMinimumSize(250,150);
    answerwindow->setWindowTitle("答案为");

    int Time1=ui->spinBox->value(),Time2=ui->spinBox_2->value();
    int cur1=ui->TimeType->currentIndex(),cur2=ui->TimeType2->currentIndex();
    int ca_Type=ui->CaculateType->currentIndex();
    QString str = Caculate(Time1,Time2,cur1,cur2,ca_Type);
    if(str=="结果为负值"){
        //设置图片
        SetImage(answerwindow,ImgfilePath);

        //设置音乐播放器
        QMediaPlayer *m_player = new QMediaPlayer(answerwindow);
        QAudioOutput *m_outPut = new QAudioOutput(answerwindow);
        m_player->setAudioOutput(m_outPut);
        m_player->setSource(QUrl::fromLocalFile("D:\\qtProject\\qtOnline\\TimeComputer\\resourse\\fanfangxiangdezhong.mp3"));
        QObject::connect(m_player,&QMediaPlayer::playbackStateChanged,answerwindow,[=](){
            if(!answerwindow->isVisible()){
                m_player->deleteLater();
            }
        });
        m_player->play();

        //设置文字
        QLabel *text = new QLabel("《反方向的钟》",answerwindow);
        text->resize(250,80);
        QFont font = text->font();
        font.setPointSize(10);
        text->setFont(font);
        text->setAlignment(Qt::AlignCenter);

        text->move(0,100);

    }
    else if(str=="只因你太美"){
        qInfo()<<"及你太美";
    }
    else{
        //设置一个文本框
        QLabel* label = new QLabel("计算结果为："+str,answerwindow);
        label->resize(100,50);
        label->move(50,25);
    }

    answerwindow->show();
}
